package com.luoxue.execsql.rest.controller;


import com.luoxue.execsql.common.contact.ExecSqlContact;
import com.luoxue.execsql.rest.service.impl.ExecSqlService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/execSql")
public class ExecSqlController {

    @Resource
    private ExecSqlService sqlExecService;

    @GetMapping("/{sqlTag}")
    public Object get(@PathVariable String sqlTag, @RequestParam Map<String, Object> param) {
        param.put("sql_tag", sqlTag);
        return sqlExecService.query(param, RequestMethod.GET);
    }

    @PostMapping("/{sqlTag}")
    public Object post(@PathVariable String sqlTag, @RequestBody Map<String, Object> param) {
        param.put("sql_tag", sqlTag);
        return sqlExecService.query(param, RequestMethod.POST);
    }

    @PutMapping("/{sqlTag}")
    public Object put(@PathVariable String sqlTag, @RequestBody Map<String, Object> param) {
        param.put("sql_tag", sqlTag);
        return sqlExecService.query(param, RequestMethod.PUT);
    }

    @DeleteMapping("/{sqlTag}/{id}")
    public Object delete(@PathVariable String sqlTag, @PathVariable String id) {
        Map<String, Object> param = new HashMap<>();
        param.put("sql_tag", sqlTag);
        param.put("id", id);
        return sqlExecService.query(param, RequestMethod.DELETE);
    }

    @DeleteMapping("/batch/{sqlTag}/{ids}")
    public Object batchDelete(@PathVariable String sqlTag, @PathVariable List<String> ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("sql_tag", sqlTag);
        param.put("ids", ids);
        return sqlExecService.query(param, RequestMethod.DELETE);
    }

}
